package com.huaxin.hxcloud.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.huaxin.common.BaseFragment;
import com.huaxin.common.Entity.HxWarningBean;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Administrator on 2016/10/25.
 * 告警Fragment
 */

public class WarningListFragment extends BaseFragment {
    private List<HxWarningBean.WarningListBean> mWarningListBeen = new ArrayList<>();

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    private HxWarningListAdapter mWarningAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_warning_list;
    }

    @Override
    protected void initView(View view) {
        ApiRequsetData();
        mRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerview.setHasFixedSize(true);
        mWarningAdapter = new HxWarningListAdapter(getContext(), mWarningListBeen);
        mRecyclerview.setAdapter(mWarningAdapter);
    }

    @Override
    public int getLoadFrameId() {
        return R.id.content;
    }

    public void ApiRequsetData() {
        updateLoadStatus(LOADING);
        Call<HxWarningBean> warningList = HxTransmitRequestHelper.getRequestApi().warningList();
        warningList.enqueue(new Callback<HxWarningBean>() {
            @Override
            public void onResponse(Call<HxWarningBean> call, Response<HxWarningBean> response) {
                HxWarningBean warningBean = response.body();

                if (response.isSuccessful() && warningBean != null) {
                    List<HxWarningBean.WarningListBean> warningListBeans = warningBean.getWarningList();
                    if (warningListBeans==null || warningListBeans.size() == 0) {
                        updateLoadStatus(LOAD_NO_DATA, "暂无警告信息");
                        return;
                    }
                    //倒序 时间最新排在最前
                    Collections.reverse(warningListBeans);
                    mWarningListBeen.addAll(warningListBeans);
                    mWarningAdapter.notifyDataSetChanged();
                    updateLoadStatus(LOAD_SUCCESS);
                } else {
                    updateLoadStatus(LOAD_ERROR, "加载错误");
                }
            }

            @Override
            public void onFailure(Call<HxWarningBean> call, Throwable t) {
                updateLoadStatus(LOAD_ERROR, "加载失败");
            }
        });
    }

}
